<template>
  <div>
    <!-- 头部导航 开始-->
    <van-nav-bar
      :title="isLogin ? '登录' : '注册'"
      left-text="返回"
      left-arrow
      @click-left="backFn"
    >
      <template #right>
        <van-icon name="ellipsis" />
      </template>
    </van-nav-bar>
    <!-- 头部导航 结束-->
    <!-- logo 开始-->
    <img src="../../assets/mlogo.png" alt="" class="logo_img" />
    <!-- logo 结束-->
    <!-- 账号密码 开始-->
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ pattern, message: '请填写正确的手机号' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <!-- 账号密码 结束-->

      <!-- 验证码 开始-->
      <Verify
        :type="2"
        width="3rem"
        height="1rem"
        fontSize=".8rem"
        :showButton="false"
        @success="alertFn(1)"
        @error="alertFn(2)"
        ref="veri"
        class="yzm"
      ></Verify>
      <!-- 验证码 结束-->
      <p @click="isLogin = !isLogin" class="zhu">
        {{ isLogin ? "立即注册" : "已有账号,立即登录" }}
      </p>
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit">{{
          isLogin ? "登录" : "注册"
        }}</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { register, login } from "../../api/";
import Verify from "vue2-verify";
export default {
  name: "Login",
  components: {
    Verify,
  },
  data() {
    return {
      username: "",
      password: "",
      pattern: /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/,
      isLogin: true,
      flag: 0,
    };
  },
  methods: {
    //返回其他页面
    backFn() {},
    //提交账号密码
    onSubmit() {
      this.$refs.veri.checkCode();
      if (this.flag) {
        if (this.isLogin) {
          login(this.username, this.password).then((data) => {
            if (data.resultCode === 200) {
              localStorage.setItem("xftoken", data.data);
              this.$router.replace("/Home");
            }
          });
        } else {
          register(this.username, this.password).then((data) => {
            if (data.resultCode === 200) {
            }
            this.isLogin = true;
          });
        }
      } else {
        this.$refs.veri.refresh();
      }
    },
    alertFn(val) {
      this.flag = val == 1 ? true : false;
    },
  },
  watch: {
    isLogin() {
      this.username = "";
      this.password = "";
      this.$refs.veri.refresh();
    },
  },
};
</script>

<style  lang="less">
.logo_img {
  width: 3.2rem;
  height: 3.2rem;
  display: block;
  margin: 2.13333rem auto 0;
}
.zhu{
  font-size: .3rem;
  color: rgb(48, 121, 223);
  margin-left: 43%;
}
.yzm .cerify-code-panel{
  display: flex!important;;
}
.yzm .cerify-code-panel .verify-code{
  width: 5rem !important;
}
.yzm .cerify-code-panel .verify-code-area .verify-input-area{
  margin: .1rem 0 0 .5rem;
  width: 3rem !important;
}
.yzm .cerify-code-panel .verify-code-area .verify-change-area{
  font-size: .2rem;
  margin-left: 1.5rem;
}
</style>